package OliveOpts;

require Exporter;
use warnings;
use strict;

our @ISA       = qw(Exporter);
our @EXPORT    = qw( &setopts &saveopts );

#--------------------------------------------------------------
# option handler routines
#--------------------------------------------------------------

sub setopts {
    my $cui = shift;
    my $w   = $cui->userdata->{wins};
    my $c   = $cui->userdata->{c};

    # build hash of enabled options from config object
    my %selected = ();
    my $i = 0;
    foreach my $opt (@{$cui->userdata->{opts}}) {
        $selected{$i} = 1 if $c->{$opt};
        $i++;
    }

    my $opts = $cui->add('opts', 'Window',
                         -border   => 1,
                         -bfg      => 'blue',
                         -title    => 'Options',
                         -height   => 13,
                         -width    => 35,
                         -centered => 1,
                        );
    $opts->{list} = $opts->add('obli', 'Listbox',
                               -values => $cui->userdata->{opts},
                               -labels => {'ec'  => 'Confirm on exit',
                                           'fol' => 'Focus story on load',
                                           'utb' => 'Sort unread to top',
                                           'sr'  => 'Skip read stories on [,]',
                                           'ws'  => 'Show story list status',
                               },
                               -multi    => 1,
                               -selected => \%selected,
                               -height   => 5,
                               -width    => 29,
                               -y        => 1,
                               -x        => 2,
                               -onchange =>  sub { saveopts($cui,$opts->{list}) },
        );
    $opts->{mins} = $opts->add('obmi', 'TextEntry',
                               -x         => 1,
                               -y         => 7,
                               -width     => 4,
                               -maxlength => 3,
                               -reverse   => 1,
                               -text      => $c->{pw},
                               -onchange  => sub { if ($opts->{mins}->get =~ /\D/) {
                                                       $cui->dialog(-message => "This field may only contain digits.",
                                                                    -title   => "Error"
                                                                   );
                                                       $opts->{mins}->text('');
                                                       $opts->{mins}->text($c->{pw}) if $c->{pw};
                                                   } else {
                                                       $c->{pw} = $opts->{mins}->get;
                                                       &OliveMiscUtil::setpollwait($cui);
                                                   }
                                                 },
        );
    $opts->{l1} = $opts->add(undef, 'Label',
                             -text => "Poll wait (in minutes)",
                             -x    => 6,
                             -y    => 7,
        );
    $opts->{okb} = $opts->add('obok', 'Buttonbox',
                              -y       => -2,
                              -x       => 24,
                              -buttons => [ { -label   => '< Save >',
                                              -value   => 1,
                                              -onpress => sub { $opts->loose_focus;
                                                                $c->write;
                                                                $cui->delete('opts');
                                                                $cui->draw },
                                            } 
                              ],
        );
    $opts->draw;
    $opts->focus;
}

sub saveopts {
    my ($cui,$o) = @_;

    my $c = $cui->userdata->{c};
    my @optlist = @{$cui->userdata->{opts}};
    my @opts    = $o->get;
    my %selopts = ();

    # build hash of selections, all turned off
    foreach my $opt (@optlist) {
        $selopts{$opt} = 0;
    }

    # loop through list of all options and list of widget-enabled
    # options. set the ones which match to 1 (on) 
    foreach my $opt (@optlist) {
        foreach my $opt2 (@opts) {
            $selopts{$opt} = 1 if ($opt eq $opt2);
        }
    }

    # now loop through all opts, turning them on or off in the
    # config onject as appropriate
    foreach my $opt (@optlist) {
        $c->{$opt} = $selopts{$opt};
    }
}

1;
